'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.ArticleCategory
  }

  // 匹配查询
  get searchKey() {
    return ['isOpen', 'app']
  }

  // 模糊查询
  get likeKey() {
    return ['categoryName']
  }

  // 查询前
  async beforeIndex() {
    const { ctx } = this
    const { query } = ctx
    const { helper } = ctx
    const filter = {}

    // 关键字模糊查询
    if (query.keyword && !query.keyword == '') {
      const reg = new RegExp(helper.toMongoRegex(query.keyword, 'i'))
      filter.$or = [{ categoryName: reg }]
    }

    return {
      filter,
      sort: {
        isTop: 'DESC',
        rank: 'ASC',
      },
    }
  }

  // 新增前
  async beforeCreate() {
    const { ctx } = this
    const { body } = ctx.request

    body.companyId = this.companyId
  }
}

module.exports = Controller
